import unittest
import requests
import jsonpath
from common.config_utils import config
from common import pubilc_api_info
from common.log_utils import logger


class TestsGetAccessTokenApi(unittest.TestCase):
    def setUp(self) -> None:
        self.hosts = config.HOSTS
        self.session = requests.session()

    def tearDown(self) -> None:
        self.session.close()

    def test_01_get_access_token(self):
        logger.info('*********************************************************')
        logger.info('*         用例test_01_get_access_token开始执行          *')
        logger.info('用例开始执行')
        url_params = {
            "grant_type": "client_credential",
            "appid": "wx629082d0f5e5cd2a",
            "secret": "754fbaa4a4efd19aa4af821cc22ce4d5"
        }
        response=pubilc_api_info.get_access_token_api(self.session,url_params)
        json_body=response.json()
        actual_result=jsonpath.jsonpath(json_body,'$.access_token')[0]
        self.assertTrue(actual_result)

    def test_02_grant_type_none(self):
        self._testMethodName='测试用例二'
        self._testMethodDoc='用例二'
        url_params = {
            "grant_type": "",
            "appid": "wx629082d0f5e5cd2a",
            "secret": "754fbaa4a4efd19aa4af821cc22ce4d5"
        }
        response=pubilc_api_info.get_access_token_api(self.session,url_params)
        json_body=response.json()
        actual_result=jsonpath.jsonpath(json_body,'$.errcode')[0]
        self.assertEqual(actual_result,40002)


if __name__ == '__main__':
    unittest.main(verbosity=2)